#!/usr/bin/perl

use strict;
use warnings;
use Carp::Clan;
use Data::Dumper;
use Storable;
use lib 'lib';
use Database;

my $db = Database->new();

# デシリアライズ
my $serialized = retrieve('serialized.dat');

# アカウント登録（事前に登録しないと関連エラー）
foreach my $account (@$serialized){

  # アカウント登録
  print "Account > ".$account->{name}."\n";
  $db->upsertAccount($account);
}

# スレッド・返信登録
foreach my $account (@$serialized){

  # スレッド確認
  if(ref($account->{thread}) eq "ARRAY"){
    my $threads = $account->{thread};
    foreach my $thread (@$threads){

      # スレッド登録
      print "Account > Thread > ".$thread->{created}."\n";
      $db->upsertThread($thread);

      # 返信確認
      if(ref($thread->{replies}) eq "ARRAY"){
        my $replies = $thread->{replies};
        foreach my $reply (@$replies){

          # 返信登録
          print "Account > Thread  > Reply > ".$reply->{created}."\n";
          $db->upsertReply($thread, $reply);
        }
      }
    }
  }
}

exit;

